package api

import (
	"context"
	"gitee.com/leo-xin/novel_distribution_sdk_go/heiyan/config"
	"net/http"
	"net/url"
)

type LinkPageApiService service

type LinkPageRequest struct {
	ctx        context.Context
	ApiService *LinkPageApiService
	pageNum    *uint64
	pageSize   *uint64
	startDate  *string
	endDate    *string
}

type LinkPageResponse struct {
	Code     *int64        `json:"code,omitempty"`
	Message  *string       `json:"message,omitempty"`
	Success  *bool         `json:"success,omitempty"`
	Redirect *bool         `json:"redirect,omitempty"`
	Data     *LinkPageData `json:"data,omitempty"`
}

type LinkPageData struct {
	PageNum     *uint64         `json:"pageNum,omitempty"`
	PageSize    *uint64         `json:"pageSize,omitempty"`
	TotalCount  *uint64         `json:"totalCount,omitempty"`
	TotalPage   *uint64         `json:"totalPage,omitempty"`
	HasPrePage  *bool           `json:"hasPrePage,omitempty"`
	HasNextPage *bool           `json:"hasNextPage,omitempty"`
	Items       []*LinkPageItem `json:"items,omitempty"`
}

type LinkPageItem struct {
	Id                *string `json:"id,omitempty"`
	Name              *string `json:"name,omitempty"`
	MediaChannelDesc  *string `json:"mediaChannelDesc,omitempty"`
	MediaChannelValue *int    `json:"mediaChannelValue,omitempty"`
	CreateTime        *string `json:"createTime,omitempty"`
	ManageId          *string `json:"manageId,omitempty"`
	ManageAccount     *string `json:"manageAccount,omitempty"`
	ManageName        *string `json:"manageName,omitempty"`
	BookId            *string `json:"bookId,omitempty"`
	BookName          *string `json:"bookName,omitempty"`
}

func (r *LinkPageRequest) Pid(pid string) *LinkPageRequest {
	r.ctx = context.WithValue(r.ctx, config.PId, pid)
	return r
}

func (r *LinkPageRequest) Secret(secret string) *LinkPageRequest {
	r.ctx = context.WithValue(r.ctx, config.Secret, secret)
	return r
}

func (r *LinkPageRequest) PageNum(pageNum uint64) *LinkPageRequest {
	r.pageNum = &pageNum
	return r
}

func (r *LinkPageRequest) PageSize(pageSize uint64) *LinkPageRequest {
	r.pageSize = &pageSize
	return r
}

func (r *LinkPageRequest) StartDate(startDate string) *LinkPageRequest {
	r.startDate = &startDate
	return r
}

func (r *LinkPageRequest) EndDate(endDate string) *LinkPageRequest {
	r.endDate = &endDate
	return r
}

func (r *LinkPageRequest) Execute() (*LinkPageResponse, *http.Response, error) {
	return r.ApiService.getExecute(r)
}

func (r *LinkPageRequest) WithLog(enable bool) *LinkPageRequest {
	if enable {
		r.ctx = context.WithValue(r.ctx, config.ContextEnableLog, true)
	}
	return r
}

func (a *LinkPageApiService) Get(ctx context.Context) *LinkPageRequest {
	return &LinkPageRequest{
		ApiService: a,
		ctx:        ctx,
	}
}

func (a *LinkPageApiService) getExecute(r *LinkPageRequest) (*LinkPageResponse, *http.Response, error) {
	var (
		localVarPath         = a.client.Cfg.GetBasePath() + "/spider/distribute/linkPage"
		localVarHTTPMethod   = http.MethodGet
		localVarPostBody     map[string]string
		localVarFormParams   = url.Values{}
		localVarHeaderParams = make(map[string]string)
		localVarQueryParams  = url.Values{}
		localVarReturnValue  *LinkPageResponse
	)

	r.ctx = a.client.prepareCtx(r.ctx)

	if ok, GenericOpenAPIError := hasReqParam(r.pageNum, "PageNum"); !ok {
		return localVarReturnValue, nil, GenericOpenAPIError
	}
	if ok, GenericOpenAPIError := hasReqParam(r.pageSize, "PageSize"); !ok {
		return localVarReturnValue, nil, GenericOpenAPIError
	}
	localVarQueryParams.Add("pageNum", toString(r.pageNum))
	localVarQueryParams.Add("pageSize", toString(r.pageSize))

	if r.startDate != nil {
		localVarQueryParams.Add("startDate", *r.startDate)
	}

	if r.startDate != nil {
		localVarQueryParams.Add("endDate", *r.endDate)
	}

	req, err := a.client.buildRequest(r.ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams)
	if err != nil {
		return localVarReturnValue, nil, err
	}

	localVarHTTPResponse, err := a.client.call(r.ctx, req, &localVarReturnValue)
	if err != nil || localVarHTTPResponse == nil {
		return localVarReturnValue, localVarHTTPResponse, err
	}
	return localVarReturnValue, localVarHTTPResponse, nil
}
